﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Transport
{
    public class SearchEngine
    {
        public string Search(string input)
        {
            try
            {
                string html = null;
                XeControl xeControl = new XeControl();
                BangGiaTrongNgayControl NgayControl = new BangGiaTrongNgayControl();
                int soCho = Utilities.GetNumberInstring(input);
                String inputfm = Utilities.FormatInputData(input);
                //Get ID dich vu
                string idDichvu = null;
                DichVuControl dichVu = new DichVuControl();

                List<dich_vu> listDVEntity = dichVu.listAll();

                foreach (dich_vu item in listDVEntity)
                {
                    int indexof = inputfm.IndexOf(item.key_word);
                    if (indexof != -1)
                    {
                        idDichvu = item.id_dich_vu;
                    }
                }
                if (soCho == 0)
                {
                    string[] LoTrinhs = input.Split(new Char[] { ',', '.', ':', '\t', '-' });
                    if (LoTrinhs.Length > 1)
                    {
                        List<BGTLTentity> result = xeControl.SearchBangGiaLTByTenLT(LoTrinhs[0],0);
                        foreach (BGTLTentity item in result)
                        {
                            html += GenHtml.genBangGiaLT(item);
                        }

                        return html;
                    }
                    else
                    {

                        if (idDichvu != null)
                        {
                            List<BangGiaTrongNgayNew> result = NgayControl.GetBangGiaTrongNgayNew(idDichvu);
                            foreach (BangGiaTrongNgayNew item in result)
                            {
                                html += GenHtml.getBangGiaNgay(item);

                            }
                        }
                        else
                        {
                            List<BGTLTentity> result1 = xeControl.SearchBangGiaLTByTenLT(input, 0);
                            foreach (BGTLTentity item in result1)
                            {
                                html += GenHtml.genBangGiaLT(item);
                            }
                        }
                        return html;
                    }
                }
                
                if (soCho != 0)
                {
                    if (idDichvu != null)
                    {
                        List<BangGiaTrongNgayNew> result = NgayControl.SearchBangGiaTrongNgay(idDichvu, soCho);
                        foreach (BangGiaTrongNgayNew item in result)
                        {
                            html += GenHtml.getBangGiaNgay(item);
                           
                        }
                        return html;
                    }
                    else
                    {
                        List<BangGiaTrongNgayNew> result = NgayControl.SearchBangGiaTrongNgayBySoCho(soCho);
                        foreach (BangGiaTrongNgayNew item in result)
                        {
                            html += GenHtml.getBangGiaNgay(item);
                            
                        }
                        List<BGTLTentity> result2 = xeControl.SearchBangGiaLTByTenLT("",soCho);
                        foreach (BGTLTentity item in result2)
                        {
                            html += GenHtml.genBangGiaLT(item);
                        }
                        return html;
                    }
                }
                return html;
            }
            catch (Exception)
            {
                return null;
            }

        }
    }
}